package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.example.common.R;
import com.example.service.CommentService;
import com.example.service.impl.CommentServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/comment/list")
public class CommentServlet extends BaseServlet {

    CommentService commentService = new CommentServiceImpl();

    /**
     * 获取评论列表
     *
     * @return
     * @throws Exception
     */
    public void getCommentTree(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.setCharacterEncoding("utf-8");
        String newsId = req.getParameter("newsId");
        String commentId = req.getParameter("commentId");

        R commentTree = commentService.getCommentTree(newsId,commentId);
        //返回结果
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(JSON.toJSONString(commentTree));
    }


    /**
     * 获取所有一级评论
     *
     * @return
     * @throws Exception
     */
    public void getFirstComment(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.setCharacterEncoding("utf-8");
        String newsId = req.getParameter("newsId");

        R commentTree = commentService.getFirstComment(newsId);
        //返回结果
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(JSON.toJSONString(commentTree));
    }


}
